package com.jacen.fzxs.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.jacen.fzxs.dao.IRightDao;
import com.jacen.fzxs.entity.RightEntity;
import com.jacen.fzxs.util.SqlCondition;
import com.jacen.fzxs.util.Utils;


@Repository
public class RightDao extends BaseDao<RightEntity> implements IRightDao {

	@Override
	public List<RightEntity> getPage(int start, int size, Map<String, String> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "SELECT * from t_right where 1=1 %s ";
		sql = String.format(sql, cond.getCondition());
		sql = SqlCondition.pageSql(sql, start, size);
		return executeForList(sql, cond.getParams().toArray());
	}

	@Override
	public int getCount(Map<String, ? extends Object> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "SELECT count(*) from t_right where 1=1 %s ";
		sql = String.format(sql, cond.getCondition());
		return Long.valueOf(executeForLong(sql, cond.getParams().toArray())).intValue();
	}
	
	private SqlCondition getPageCondition(
			Map<String, ? extends Object> mapParams) {
		SqlCondition cond = new SqlCondition();
		if (Utils.paramIsNotEmpty(mapParams.get("pid")))
			cond.and().equal("up_right_id", mapParams.get("pid"));
		return cond;
	}

	@Override
	public List<RightEntity> getListByUpId(String upId) {
		String sql ="select * from %s where FIND_IN_SET(id, queryChildrenAreaInfo('%s')); ";
		sql = String.format(sql, tableName,upId);
		return executeForList(sql);
	}

	@Override
	public List<RightEntity> getListByRole(String roleId) {
		String sql ="select t.* from t_right t left join t_role_right rr on t.id=rr.right_id where  rr.role_id = '%s' ";
		sql = String.format(sql, roleId);
		return executeForList(sql);
	}

	@Override
	public List<String> getIdsByRole(String roleId) {
		String sql ="select t.id from t_right t left join t_role_right rr on t.id=rr.right_id where  rr.role_id = '%s' ";
		sql = String.format(sql, roleId);
		return getJdbcTemplate().queryForList(sql, String.class);
	}
	

}
